/*=============================================================
(c) all rights reserved

GraydonDataServices examples

================================================================*/
using System;
using System.Web.Services.Protocols;

/**
 * @author denisreilly
 *
 * Runnable .net 2.0 client for Graydon Company Data Webservice
 * 
 * Operation : PlaceOrder
 * 
 * Argument0 = Partner Userid
 * Argument1 = Partner Password
 * Argument2 = Company Product Identifier
 */
class PlaceOrder{   
   public static void Main(string[] args)
   {
      
       try
	   {
	      string partnerUserid             = null;
	      string partnerPassword	       = null;
	      string companyProductIdentifier  = null;
	      
	      // retrieve arguments
	      if (args.Length == 0)
			throw new Exception("Invalid arguments: Please specify match arguments");
			
	      // populate match parameters with arguments
	      partnerUserid               = args[0].Trim();
	      partnerPassword             = args[1].Trim();
	      companyProductIdentifier    = args[2].Trim();
	            
	      GraydonCompanyData graydonCompanyData = new GraydonCompanyData();
	      
	      // create the authentication parameters
	      Authentication_ParametersType authentication_Parameters                                    = new Authentication_ParametersType();
	      authentication_Parameters.PartnerUserId                                                    = partnerUserid;
	      authentication_Parameters.PartnerPassword                                                  = partnerPassword;
	      
	      // create the parameters
	      PlaceOrder_ParametersType placeOrder_Parameters                                            = new PlaceOrder_ParametersType();
	      placeOrder_Parameters.Authentication_Parameters                                            = authentication_Parameters;
	      placeOrder_Parameters.CompanyProductIdentifier                                             = companyProductIdentifier;

	      // Begin the Async call to "placeOrder"
	      IAsyncResult ar = graydonCompanyData.BeginplaceOrder(placeOrder_Parameters, null, graydonCompanyData);
	      
	      // wait for response
	      ar.AsyncWaitHandle.WaitOne();
	      
	      PlaceOrder_ResultType placeOrder_Result = null;
	      
	      try
          {
             // get the results of the match
             placeOrder_Result = graydonCompanyData.EndplaceOrder(ar);
          }
          catch (SoapException soapE)
          {
            Console.WriteLine(" ");
            // SOAP 1.1 Fault info
            Console.WriteLine("SOAP 1.1 fault...");
            Console.WriteLine("Code:/t/t" + soapE.Code.ToString());
            Console.WriteLine("Actor:/t/t" + soapE.Actor);
            Console.WriteLine("Detail:/t/t" + soapE.Detail.InnerXml);
            throw new Exception("Fault occurred whilst accessing the webservice.");
          }
          
         // get the service log details
         serviceLog(placeOrder_Result.Service_Log);
         
		 // check if an order status has been returned
		 if (placeOrder_Result.OrderStatus != null)
		 {	
			// get the order status detail
			OrderStatusType orderStatus = placeOrder_Result.OrderStatus;
			Console.WriteLine("Order Status\t\t\t:");
			Console.WriteLine("  Order Reference\t\t: " + orderStatus.OrderReference);								       // output company name
			Console.WriteLine("  Order State\t\t\t: " + orderStatus.OrderState);									       // output order state
			Console.WriteLine("  Date Ordered\t\t\t: " + "{0:d}", orderStatus.DateOrdered);				                   // output date ordered
			Console.WriteLine("  Date Completed\t\t: " + "{0:d}", orderStatus.DateCompleted);			                   // output date completed
			
			// get some order details
			if (orderStatus.Order != null)
			{
				OrderType order = orderStatus.Order;
				Console.WriteLine("Order \t\t\t:");			
				Console.WriteLine("  Company Name\t\t\t: " + order.Name);	      											// output company name
				Console.WriteLine("  Company Identifier\t\t: " + order.CompanyIdentifier.Identifier);			            // output company identifier
			}
		 }
	  }  
      catch (Exception e)
      {
	     Console.WriteLine("Exception : " + e);
      }

   }
   
   	/**
	* Service log output
	*
	*/
	private static void serviceLog(Service_LogType service_Log)
	{
		Console.WriteLine("Service_Log :");																	
		Console.WriteLine("  PartnerUserId\t\t: " + service_Log.PartnerUserId);												    // output partner userid
		Console.WriteLine("  SessionId\t\t: " + service_Log.SessionID);														    // output sessionId
		Console.WriteLine("  Transaction Identifier: " + service_Log.TransactionIdentifier);									// output transaction identifier
		Console.WriteLine("  Request Date\t\t: " + "{0:d}", service_Log.RequestTimestamp.Date);				                    // output request date
		Console.WriteLine("  Request Time\t\t: " + "{0:t}", service_Log.RequestTimestamp.Time);							        // output request time
		Console.WriteLine("  Response Date\t\t: " + "{0:d}", service_Log.ResponseTimestamp.Date);				                // output response date
		Console.WriteLine("  Response Time\t\t: " + "{0:t}", service_Log.ResponseTimestamp.Time);								// output response time
		Console.WriteLine(" ");
	}
   
}